package com.kucai.yifaqapp.presenter.fragment;

import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.ResGameDetails;
import com.kucai.yifaqapp.model.network.entity.res.ResMoney;
import com.kucai.yifaqapp.model.network.entity.res.ResSetbet;
import com.kucai.yifaqapp.model.network.entity.res.Resjsliuhecai;
import com.kucai.yifaqapp.model.network.entity.res.Resxgliuhecai;
import com.kucai.yifaqapp.model.network.entity.res.Resxyliuhecai;
import com.kucai.yifaqapp.model.network.entity.res.betbean;
import com.kucai.yifaqapp.model.network.entity.res.uploadPicBean;
import com.kucai.yifaqapp.model.network.entity.res.uploadPicBena;
import com.kucai.yifaqapp.ui.fragment.gameFragment.LHCYFFragment;
import com.kucai.yifaqapp.util.websocket.WebSocketUtils2;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/12/10.
 */

public class LHCYFFragmentPresenter extends BaseFragmentPresenter<LHCYFFragment> {


    public LHCYFFragmentPresenter(LHCYFFragment mFragment) {
        super(mFragment);
    }

    //获取游戏详情
    public void getgamesdetail(String gameid) {
        HttpJsonUtils.getHttpApiInstance()
                .getgamesdetail(HttpAdress.getuseAdress() + "api/m/lottery/getNextIssue.do", gameid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResGameDetails>() {
                    @Override
                    protected void onCustomNext(ResGameDetails resGameDetails) {
                        if (null != resGameDetails.data && resGameDetails.code == 0) {
                            if (("极速六合彩").equals(resGameDetails.data.game_name)) {
                                mFragment.liuhecai(resGameDetails);
                            } else {
                                mFragment.liuhecaifail();
                            }

                        } else {
                            mFragment.liuhecaifail();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        mFragment.liuhecaifail();
                    }
                });
    }//获取游戏详情

    public void getgamesdetail1(String gameid) {
        HttpJsonUtils.getHttpApiInstance()
                .getgamesdetail(HttpAdress.getuseAdress() + "api/m/lottery/getNextIssue.do", gameid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResGameDetails>() {
                    @Override
                    protected void onCustomNext(ResGameDetails resGameDetails) {
                        if (null != resGameDetails.data && resGameDetails.code == 0) {
                            if (("极速六合彩").equals(resGameDetails.data.game_name)) {
                                mFragment.liuhecai1(resGameDetails);
                            } else {
                                mFragment.liuhecaifail();
                            }

                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    //下注
    public void Betbet(final String data, String token) {
//        HttpJsonUtils.getHttpApiInstance()
//                .Betbet(HttpAdress.getuseAdress() + "api/m/lottery/bet.do", data, token)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CustomObserver<ResSetbet>(mFragment) {
//                    @Override
//                    protected void onCustomNext(ResSetbet resOods) {
//
//                        if ("200".equals(resOods.code)) {
//                            mFragment.betsuccses();
//                        }
//                        else {
//                            Toast.makeText(mFragment.getContext(), resOods.msg, Toast.LENGTH_SHORT).show();
//                        }
//                    }
//
//                    @Override
//                    protected void onSubscribeCustom(Disposable disposable) {
//                    }
//
//                    @Override
//                    protected void onErrorCustom(Throwable e) {
//                    }
//                });
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();
        betbean bean = new betbean();
        bean.data = data;
        bean.token = token;
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , json);
        final Request request = new Request.Builder()
                .url(HttpAdress.getuseAdress() + "api/m/lottery/bet.do")//请求的url
                .post(requestBody)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String resSetbet = response.body().string();
                Log.e("xdh",resSetbet);
                ResSetbet resSetbet1 = new Gson().fromJson(resSetbet, ResSetbet.class);
                if ("200".equals(resSetbet1.code)) {
                    mFragment.betsuccses();
                } else {

                }
            }
        });
    }

    //获取余额
    public void getmoney(String token) {
        HttpJsonUtils.getHttpApiInstance()
                .getmoney(HttpAdress.getuseAdress() + "api/m/user/getMoney", token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResMoney>() {
                    @Override
                    protected void onCustomNext(ResMoney resOods) {
                        if (resOods.code == 0) {
                            mFragment.moneychange(resOods);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

    public void getjisuliuhecaiddes(final String gameid, String token) {
        HttpJsonUtils.getHttpApiInstance()
                .getjisuliuhecaiddes(HttpAdress.getuseAdress() + "api/m/pc/bet/getGameOdds", gameid, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Resjsliuhecai>(mFragment) {
                    @Override
                    protected void onCustomNext(Resjsliuhecai resOods) {

                        if (resOods.code.equals("0")) {
                            mFragment.setxyliuhecaiddes(resOods.data);
                        } else {
                            Toast.makeText(mFragment.getContext(), "赔率获取失败，请重新进入", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

}